package td3.view;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.tree.TreeNode;

import td3.model.AppModel;
import td3.model.Contact;
import td3.model.ContactFileTransferHandler;
import td3.model.ContactTreeModel;

public class ContactEditPane extends JPanel {
	private static final long serialVersionUID = 1L;

	private JEditorPane editor;
	private JTextField name;
	private JTextField email;
	private JLabel nameLabel;
	private JLabel emailLabel;
	private JButton validate;
	private JLabel img;

	public ContactEditPane(final AppModel appModel) {
		super(new BorderLayout());
		this.setTransferHandler(new ContactFileTransferHandler(appModel));
		editor = new JEditorPane();
		editor.setEditable(true);

		name = new JTextField();
		email = new JTextField();
		img = new JLabel();
		nameLabel = new JLabel();
		nameLabel.setText("Nom : ");
		nameLabel.setLabelFor(name);
		emailLabel = new JLabel();
		emailLabel.setText("Email : ");
		emailLabel.setLabelFor(email);

		validate = new JButton("Valider");
		validate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				appModel.getCurrentContact().setMail(email.getText());
				appModel.getCurrentContact().setNom(name.getText());
				appModel.getContactTreeModel().reload();
			}
		});
		JPanel textControlsPane = new JPanel();
		GridBagLayout gridbag = new GridBagLayout();

		textControlsPane.setLayout(gridbag);

		JLabel[] labels = { nameLabel, emailLabel };
		JTextField[] textFields = { name, email };
		addLabelTextRows(labels, textFields, gridbag, textControlsPane);

		this.add(textControlsPane, BorderLayout.NORTH);
		this.add(validate, BorderLayout.SOUTH);
		img = new JLabel();
		this.add(img);
		img.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent e) {
				System.out.println("clic");
				File currentFile;
				JFileChooser jFileChooser = new JFileChooser();
				FileNameExtensionFilter filter = new FileNameExtensionFilter(
						"*.jpg", "jpg");
				jFileChooser.addChoosableFileFilter(filter);
				jFileChooser.setAcceptAllFileFilterUsed(false);
				int returnVal = jFileChooser
						.showOpenDialog(ContactEditPane.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					currentFile = jFileChooser.getSelectedFile();
					try {
						
						appModel.getCurrentContact().setIcone(currentFile.getPath());
						img.setIcon(new ImageIcon(appModel.getCurrentContact().getIcone()));
					} catch (Exception ex) {
						System.err.println("OPENING FILE FAILED");
					}
				}

			}
		});

		appModel.addPropertyChangeListener(new PropertyChangeListener() {

			@Override
			public void propertyChange(PropertyChangeEvent event) {
				if (event.getPropertyName().equals(
						AppModel.Property.CURRENT_CONTACT.name())) {
					if (event.getNewValue() != null
							&& event.getNewValue() instanceof Contact) {
						Contact contact = (Contact) event.getNewValue();
						name.setText(contact.getNom());
						email.setText(contact.getMail());
						img.setIcon(new ImageIcon(contact.getIcone()));
					} else {
						name.setText("");
						email.setText("");
						img.setIcon(null);

					}
				}
			}
		});

	}

	private void addLabelTextRows(JLabel[] labels, JTextField[] textFields,
			GridBagLayout gridbag, Container container) {
		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.EAST;
		int numLabels = labels.length;

		for (int i = 0; i < numLabels; i++) {
			c.gridwidth = GridBagConstraints.RELATIVE; // next-to-last
			c.fill = GridBagConstraints.NONE; // reset to default
			c.weightx = 0.0; // reset to default
			container.add(labels[i], c);

			c.gridwidth = GridBagConstraints.REMAINDER; // end row
			c.fill = GridBagConstraints.HORIZONTAL;
			c.weightx = 1.0;
			container.add(textFields[i], c);
		}
	}
}
